package com.adds.lvds.controller.code;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.model.code.SysCode;
import com.adds.lvds.security.BaseController;
import com.adds.lvds.service.impl.code.SysCodeService;

@Controller
@RequestMapping(value = "/code/")
public class SysCodeController extends BaseController<SysCode> {
	@Inject
	private SysCodeService sysCodeService;

	/**
	 * 跳转到代码设置列表页
	 * 
	 * @return
	 */
	@RequestMapping("index")
	public String index(Model model, HttpServletRequest request, 
			@Param(value = "typeName")String typeName) {
		String htmlBut = (String) request.getAttribute("htmlBut");
		model.addAttribute("typeName", typeName);
		model.addAttribute("htmlBut", htmlBut);
		return "/code/listSysCode";
	}

	/**
	 * 加载代码设置列表数据
	 * 
	 * @param response
	 * @param sysCodeType 获取查询条件
	 * @param page 获取当前页数,page是jqGrid自带参数
	 * @param rows 获取每页显示行数,rows是jqGrid自带参数
	 * @param sidx 获取排序列名,sidx是jqGrid自带参数
	 * @param sord 获取排序方式,sord是jqGrid自带参数
	 * @param typeName
	 */
	@ResponseBody
	@RequestMapping(value = "loadDataGrid")
	public Map<String, Object> loadDataGrid(HttpServletRequest request,
			HttpServletResponse response,
			@Param(value = "sysCode") SysCode sysCode,
			@Param(value = "page") String page,
			@Param(value = "rows") String rows,
			@Param(value = "sidx") String sidx,
			@Param(value = "sord") String sord,
			@Param(value = "typeName") String typeName) {
		Map<String, Object> map = new HashMap<String, Object>();
		sysCode.setTypeName(typeName);
		pageView = sysCodeService.querySysCode(
				getPageView(page, rows, sidx, sord), sysCode);
		map.put("rows", pageView.getRecords());
		map.put("page", pageView.getPageNow());
		map.put("total", pageView.getPageCount());
		return map;
	}
	/**
	 * 添加代码值数据
	 * 
	 * @param sysCode
	 * @param typeName
	 * @return
	 */
	@RequestMapping("addSysCode")
	@ResponseBody
	public Map<String, Object> addSysCode(
			@Param(value = "sysCode") SysCode sysCode,
			@Param(value = "typeName") String typeName) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = sysCodeService.insertSysCode(sysCode);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "录入代码值数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "录入代码值数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 跳转到修改页面
	 * 
	 * @param model
	 * @param codeName
	 * @param typeName
	 * @return
	 */
	@RequestMapping("editSysCode")
	public String editSysCode(Model model,
			@Param(value = "codeName") String codeName,
			@Param(value = "typeName") String typeName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("typeName", typeName);
		map.put("codeName", codeName);
		SysCode sysCode = sysCodeService.getSysCodeByMap(map);
		model.addAttribute("sysCode", sysCode);
		return "/code/editSysCode";
	}

	/**
	 * 编辑代码值数据
	 * 
	 * @param model
	 * @param sysCode
	 * @param typeName
	 * @return
	 */
	@ResponseBody
	@RequestMapping("doEditSysCode")
	public Map<String, Object> doEditSysCode(Model model, SysCode sysCode, String typeName) {
		sysCode.setTypeName(typeName);
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = sysCodeService.updateSysCode(sysCode);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "编辑代码值数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "编辑代码值数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}
	/**
	 * 删除
	 * 
	 * @param model
	 * @param typeName
	 * @param codeNames
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deleteByCodeName")
	public Map<String, Object> deleteByCodeName(Model model,
			@Param(value = "typeName") String typeName,
			@Param(value = "codeNames") String codeNames) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = CommUtils.SUCC;
		String[] codeName = codeNames.split(",");
		for (int i = 0; i < codeName.length; i++) {
			if (!StringUtils.isEmpty(codeName[i])) {
				map.put("codeName", codeName[i]);
				map.put("typeName", typeName);
				errcode = sysCodeService.deleteSysCode(map);
			}
		}
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "删除代码值数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "删除代码值数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}


}
